package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Organization;
import com.daka.entity.OrganizationGroupVerify;
import com.daka.enumerate.StatusCode;
import com.daka.service.OrganizationGroupVerifyService;
import com.daka.service.OrganizationService;
import com.daka.vo.db.OrgGroupVerifyVo;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/orgVerify")
@Slf4j
public class OrganizationGroupVerifyController {

    @Resource(name = "organizationGroupVerifyService")
    private OrganizationGroupVerifyService organizationGroupVerifyService;

    @Resource
    private OrganizationService organizationService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<OrgGroupVerifyVo> list(HttpSession session) {
        int orgId;
//        if (organization.getOrgId() != 0) {
//            orgId = organization.getOrgId();
//        } else {
        orgId = Integer.parseInt(session.getAttribute("orgId").toString());
//        }

        Organization organizationResult = organizationService.selectById(orgId);

        List<OrgGroupVerifyVo> orgGroupVerifyVo = organizationGroupVerifyService.selectByParentCode(organizationResult.getOnePass());

        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), orgGroupVerifyVo);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/checkFail", consumes = "application/json")
    @Login
    public BaseResponseVo checkFail(@RequestBody OrganizationGroupVerify organizationGroupVerify) {

        organizationGroupVerify.setStatus(-1);//-1 已拒绝
        organizationGroupVerify.setVerifyTime(new Timestamp(System.currentTimeMillis()));

        int resultCount = organizationGroupVerifyService.updateById(organizationGroupVerify);

        if (resultCount == 1) {
            return new BaseResponseVo(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
        } else {
            return new BaseResponseVo(StatusCode.UPDATE_ERROR.getStatusCode(), StatusCode.UPDATE_ERROR.getStatusMessage());
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/checkSuccess", consumes = "application/json")
    @Login
    public BaseResponseVo checkSuccess(@RequestBody OrganizationGroupVerify organizationGroupVerify) {
        organizationGroupVerify.setStatus(2);//-1 已接受
        organizationGroupVerify.setVerifyTime(new Timestamp(System.currentTimeMillis()));
        int resultCount = organizationGroupVerifyService.verifySuccess(organizationGroupVerify);

        if (resultCount == 1) {
            return new BaseResponseVo(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
        } else {
            return new BaseResponseVo(StatusCode.UPDATE_ERROR.getStatusCode(), StatusCode.UPDATE_ERROR.getStatusMessage());
        }
    }

}
